/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Padda1
 */
import javax.swing.JOptionPane;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class Compiler 
{
    /**
    * Constructor
    */
    Compiler()
    {

    }//Compiler

    /**
    * Compiles the file given as parameter and returns true if the file compiled successfully.
    * @param filename String. The file location. Example Example.java, .../project/source/filename
    * @return true if compiled false otherwise.
    */
    boolean compile (String filename)
    {
        boolean result = false;
        try
        {
            if (!filename.equals(""))
            {
                JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
                if (compiler == null)
                {
                    JOptionPane.showMessageDialog(null, "Compiler was not found.", "Compiler Not Found",JOptionPane.ERROR_MESSAGE);
                }//if
                else
                {
                    int compilationResult = compiler.run(null, null, null, filename);
                    if(compilationResult == 0)
                    {
                        result = true;
                    }//if
                    else
                    {
                        result = false;
                    }//else
                }//else
            }//if
            else
            {
                result = false;
            }//else
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, filename + "\n" + e.toString(), "Compile",JOptionPane.ERROR_MESSAGE);
        }
        return result;
    }//compile
}//class Compiler
